﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Entity;

namespace Controller
{
    public class StockItemController:ControllerBase
    {

        public void UpdateStockItemStock(int stockItemId, int p)
        {
            StockItem stockItem = (from s in Sdc.StockItems
                                   where s.Id == stockItemId
                                   select s).SingleOrDefault();
            stockItem.Stock = stockItem.Stock + p;
            Sdc.SubmitChanges();
        }

        public IList<StockItemEntity> FindBatchNoByProductId(int productId)
        {
            IList<StockItem> stockItems = (from s in Sdc.StockItems
                                           where s.ProductId == productId
                                           select s).ToList();
            IList<StockItemEntity> stockItemEntities=new List<StockItemEntity>();
            foreach (StockItem stockItem in stockItems)
            {
                StockItemEntity stockItemEntity = TransformStockItemToStockItemEntity(stockItem);
                stockItemEntities.Add(stockItemEntity);
                
            }
            return stockItemEntities;
        }
        public static StockItemEntity TransformStockItemToStockItemEntity(StockItem stockItem)
        {
            StockItemEntity stockItemEntity=new StockItemEntity();
            stockItemEntity.BatchNo = stockItem.BatchNo;
            stockItemEntity.Id = stockItem.Id;
            stockItemEntity.ProductId = stockItem.ProductId;
            stockItemEntity.Stock = stockItem.Stock;
            return stockItemEntity;
        }

        public static StockItem TransformStockItemEntityToStockItem(StockItemEntity stockItemEntity)
        {
            StockItem stockItem=new StockItem();
            stockItem.BatchNo = stockItemEntity.BatchNo;
            stockItem.ProductId = stockItemEntity.ProductId;
            stockItem.Stock = stockItemEntity.Stock;
            return stockItem;
        }

        public int AddStockItem(StorageEntity storageEntity)
        {
            IList<StockItem> stockItems = (from s in Sdc.StockItems
                                           select s).ToList();
           
            string batchNo =  storageEntity.BatchNo;
            int productId = storageEntity.ProductId;
            int count = 0;
            int stockItemId=0;

            //To see whether there is an exsiting record, if yes, update the stock only, if no, insert a new record.
            foreach (StockItem stockItem in stockItems)
            {
                if (stockItem.BatchNo == batchNo.Trim()&&stockItem.ProductId==productId)
                {
                    count++;
                    stockItemId = stockItem.Id;
                    break;
                }
                
            }
            if(count==0)
            {
                StockItemEntity stockItemEntity=new StockItemEntity();
                stockItemEntity.BatchNo = storageEntity.BatchNo;
                stockItemEntity.ProductId = storageEntity.ProductId;
                stockItemEntity.Stock = storageEntity.Quantity;
                StockItem stockItem = TransformStockItemEntityToStockItem(stockItemEntity);
                Sdc.StockItems.InsertOnSubmit(stockItem);
                Sdc.SubmitChanges();
                stockItemId = stockItem.Id;
                return stockItemId;
            }
            else
            {
                //select the existing record, ready for update its stock.
                StockItem stockItem = (from s in Sdc.StockItems
                                       where s.Id == stockItemId
                                       select s).SingleOrDefault();
                StockItemEntity stockItemEntity = new StockItemEntity();
                stockItemEntity.Stock = storageEntity.Quantity;
                stockItem.Stock += stockItemEntity.Stock;
                Sdc.SubmitChanges();
                stockItemId = stockItem.Id;
                return stockItemId;
            }
        }

        public IList<StockItemEntity> FindStockItemByProductId(int productId)
        {
            IList<StockItem> stockItems = (from s in Sdc.StockItems
                                           where s.ProductId == productId
                                           select s).ToList();
            IList<StockItemEntity> stockItemEntities=new List<StockItemEntity>();
            foreach (StockItem stockItem in stockItems)
            {
                StockItemEntity stockItemEntity = TransformStockItemToStockItemEntity(stockItem);
                stockItemEntities.Add(stockItemEntity);
            }
            return stockItemEntities;
        }
    }
}
